import sys
import time
import copy
import pygame
import numpy as np
from Object import Object
from collision_detecting_tools import mark_map


def judge_success():
    flag = False
    distance1 = sum((points_list_m[2]-points_list_f[5])**2)
    distance2 = sum((points_list_m[3]-points_list_f[4])**2)
    distance3 = sum((points_list_m[4]-points_list_f[3])**2)
    distance4 = sum((points_list_m[5]-points_list_f[2])**2)
    if distance1 < 4 and distance2 < 4 and distance3 < 4 and distance4 < 4:
        flag = True
        # pygame.draw.polygon(window, (0, 0, 255), [[100, 100], [200, 100], [200, 200]], 0)
        # pygame.display.update()
    return flag


if __name__ == '__main__':
    pygame.init()
    pygame.display.set_caption("显示图形")

    window = pygame.display.set_mode((600, 600))
    window.fill((255, 255, 255))
    pygame.display.update()
    map_m = np.array([[0 for _ in range(600)] for _ in range(600)])
    map_f = np.array([[0 for _ in range(600)] for _ in range(600)])

    object_m = Object(object_type='m', color=(255, 0, 0))
    object_f = Object(object_type='f', color=(0, 255, 0))

    action = [0, 0, 0]
    object_m.update_position(pygame, window, action, map_m, map_f)
    object_f.update_position(pygame, window, action, map_m, map_f)

    points_list_m = [[0, 0] for _ in range(8)]
    points_list_f = [[0, 0] for _ in range(8)]
    while True:
        # time.sleep(0.1)

        action[0], action[1], action[2] = map(int, input("object_m action=").split())
        points_list_m = object_m.update_position(pygame, window, action, map_m, map_f)
        # print(points_list_m[3])
        map_m = mark_map(np.array(points_list_m).astype(int), 599, 599)

        # from PIL import Image
        # im = Image.fromarray(np.uint8((map_f+map_m) * 255))
        # im.show()

        if judge_success():
            print("success")

        action[0], action[1], action[2] = map(int, input("object_f action=").split())
        points_list_f = object_f.update_position(pygame, window, action, map_m, map_f)
        # print(points_list_f[5])
        map_f = mark_map(np.array(points_list_f).astype(int), 599, 599)

        # from PIL import Image
        # im = Image.fromarray(np.uint8((map_f+map_m) * 255))
        # im.show()

        if judge_success():
            print("success")

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()
